﻿
Imports System.Data.Sql
Imports System.Data.SqlClient
''' <summary>
''' 物料清單
''' </summary>
''' <remarks>songrenhua</remarks>
''' 

Public Class Frm_MauBomBill
    Implements IDServer.IDServer.IDServer
    Public ActionHelp As IDServer.IDServer.ActionBtn = IDServer.IDServer.ActionBtn.ActionHelp
    Private mDataset As DataSet
    Private MasterDS As DataSet
    Private SelectNode As TreeNode
    Private DS As DataSet
    Private CurrentRowIndex As Integer
    Private selectObj As TextBox '選中的TextBox
    Private WithEvents MainForm As Frm_MainForm
    Public Fields, strFileName, strCheckedNode As String
    Private BomTable As DataTable
    Private Img As ImageList
    Private GoodsDs As DataSet
    Private TabSupplier As DataTable



    Public Property SelectObject() As Frm_MainForm
        Get
            Return MainForm
        End Get
        Set(ByVal value As Frm_MainForm)
            MainForm = value
        End Set
    End Property

    Private Function ReturnCaption(ByVal index As Integer) As Hashtable
        Dim Hash As New Hashtable

        Select Case index
            Case 1

                Hash.Add("EnName", "英文名")
                Hash.Add("BarCode", "貨品條碼")
                Hash.Add("Code", "編碼")
                Hash.Add("GoodsType", "貨品類型")
                Hash.Add("SupperItem", "貨品型號")
                Hash.Add("Color", "顏色")
                Hash.Add("Spec", "規格")
                Hash.Add("Unit", "單位")
                Hash.Add("ShortName", "簡稱")
                Hash.Add("CnName", "中文名")
                
            Case 2
                'BarCode, GoodsType, BillBarCode, Unit, Color, Spec, Qty, Description, BulidDate, BulidMan, BulidComputer
                Hash.Add("BarCode", "貨品條碼")
                Hash.Add("GoodsType", "物料類型")
                Hash.Add("BillBarCode", "物料編號")
                Hash.Add("Color", "顏色")
                Hash.Add("Spec", "規格")
                Hash.Add("Qty", "數量")
                Hash.Add("Unit", "單位")
                Hash.Add("Brand", "品牌")
                Hash.Add("Supplier", "供應商代碼")
                Hash.Add("SupplierName", "供應商")
                Hash.Add("Description", "物料描述")
                Hash.Add("BulidDate", "建立日期")
                Hash.Add("BulidMan", "建立者")
                Hash.Add("BulidComputer", "建立電腦")

            Case 3

                Hash.Add("BarCode", "貨品條碼")
                Hash.Add("GoodsType", "物料名稱")
                Hash.Add("BillBarCode", "物料編號")
                Hash.Add("Color", "顏色")
                Hash.Add("Spec", "規格")
                Hash.Add("Qty", "數量")
                Hash.Add("Unit", "單位")
                Hash.Add("Brand", "品牌")
                Hash.Add("Supplier", "供應商代碼")
                Hash.Add("SupplierName", "供應商")
                Hash.Add("Description", "物料描述")

        End Select

        Return Hash
    End Function

    Private Sub Lode_DGV()
        Try
            Dim SQLSTR As String
            Dim DS As DataSet
            SQLSTR = "select BarCode, GoodsType, BillBarCode, Unit, Color, Spec, Qty, Brand,Supplier,SupplierName,Description, BulidDate, BulidMan, BulidComputer from BSC_BOM where 1<>1;"
            SQLSTR += "select BarCode, GoodsType, BillBarCode, Unit, Color, Spec, Qty, Brand,Supplier,SupplierName,Description  from BSC_BOM where 1<>1 "
            DS = Exec(SQLSTR, E_ExecType.DataSet)
            Me.DGV_BomBill.DataSource = DS.Tables(0)
            BindDataGridViewCaption(Me.DGV_BomBill, Me.ReturnCaption(2))
            Me.DGV_BomBill_1.DataSource = DS.Tables(1)
            BindDataGridViewCaption(Me.DGV_BomBill_1, Me.ReturnCaption(3))
        Catch ex As Exception
            MessageBox.Show(ex.Message.ToString, Msg.CompanyName)
        End Try
    End Sub

    Public Sub SetBomTable()
        BomTable = New DataTable
        With BomTable.Columns
            .Add("BarCode", GetType(String))
            .Add("GoodsType", GetType(String))
            .Add("BillBarCode", GetType(String))
            .Add("Unit", GetType(String))
            .Add("Color", GetType(String))
            .Add("Spec", GetType(String))
            .Add("Qty", GetType(String))
            .Add("Brand", GetType(String))
            .Add("Supplier", GetType(String))
            .Add("SupplierName", GetType(String))
            .Add("Description", GetType(String))
        End With
    End Sub

    Private Sub BtnFind_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnFind.Click
        SelectData(True)
    End Sub

    Private Sub SelectData(ByVal B As Boolean)
        Try
            Dim SQL As String = String.Empty
            SQL += "SELECT EnName,Replace(BarCode,'*','') as BarCode,Code,GoodsType = c.fFullName,SupperItem,Unit=e.ListA,Color = b.ColorCName," & vbCrLf
            SQL += "Spec = d.SizeSName,ShortName,CnName from BSC_GOODS a " & vbCrLf
            SQL += "LEFT JOIN  bsc_colordefine b on a.Color = b.ColorCode " & vbCrLf
            SQL += "LEFT JOIN bsc_goodstype c ON a.goodstype = c.id " & vbCrLf
            SQL += "LEFT JOIN  bsc_sizedefine d ON a.spec = d.sizecode " & vbCrLf
            SQL += "LEFT JOIN (SELECT c.itemcode,d.listcode,d.lista FROM   bsc_otherdefinea c,bsc_otherdefineb d " & vbCrLf
            SQL += "WHERE  c.itemcode = 'UnitDefin'AND c.id = d.itemno) e ON a.unit =e.listcode " & vbCrLf
            If B = True Then
                SQL += "WHERE 1=1 " & vbCrLf 'and EnName <>'' 
                If Me.TBBarCode.Text.Trim <> "" Then
                    SQL += " AND  Replace(BarCode,'*','') = '" & Me.TBBarCode.Text.Trim & "'" & vbCrLf
                End If
                If Me.TBENName.Text.Trim.Length > 0 Then
                    SQL += " AND  enname = '" & Me.TBENName.Text.Trim & "'" & vbCrLf
                End If
                If Me.txtGoodsType1.Text.Trim.Length > 0 Then
                    SQL += " and c.fFullName like '" & Me.txtGoodsType1.Text & "%'"
                End If
                If Me.Chk1.Checked = True Then
                    SQL += " and a.BulidDate between '" & Me.DTPfrom.Text & "' and  '" & Me.DTPto.Text & "'"
                End If
                If Me.TxtOperator.Text.Length > 0 Then
                    SQL += " and a.BulidBy like '" & Me.TxtOperator.Text & "%'"
                End If
            Else
                SQL += "WHERE 1<>1"
            End If
            DS = Exec(SQL, E_ExecType.DataSet)
            Me.DGV_Goods.DataSource = DS.Tables(0)
            BindDataGridViewCaption(Me.DGV_Goods, Me.ReturnCaption(1))
            If B = True Then
                IsFind = FindRecord()
                SetButton(ButtonType.Browse, IsFind)
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message, Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

    Private Sub EditData(ByVal B As Boolean)
        Try
            If EditEnable = False Then
                MessageBox.Show("對不起，您沒有修改權限", Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Information)
                Exit Sub
            End If

            If Me.DGV_BomBill.Rows.Count <= 0 Then
                Exit Sub
            End If

            If DS.Tables(0).Rows.Count = 0 Then
                Exit Sub
            End If
            'If Me.DGV_Goods.SelectedCells.Count > 0 Then
            '    Dim Barcode As String = Me.DGV_Goods.Rows(Me.DGV_Goods.CurrentRow.Index).Cells(0).Value.ToString & ""
            '    CurrentRowIndex = Me.DGV_Goods.CurrentRow.Index
            '    FillText(Barcode)
            'End If
            Me.TabControl1.SelectedIndex = 1
            ActionHelp = IDServer.IDServer.ActionBtn.ActionEdit
            EnabledControls(True)
            SetButton(ButtonType.Edit, False)
            SetMainMenu(False)

            FillEdit()
            Me.TBGoodsType_1.Enabled = False
            Me.CBGoodsType.Enabled = False




        Catch ex As Exception
            MsgBox(ex.Message, Msg.CompanyName)

        End Try

    End Sub

    Private Sub FillEdit()
        Try
            If Me.DGV_BomBill_1.Rows.Count <= 0 Then
                Exit Sub
            End If
            Dim SRT As String = String.Empty
            Dim DT As DataTable

            Dim BillBarCode As String = Me.DGV_BomBill_1.Rows(Me.DGV_BomBill_1.CurrentRow.Index).Cells(2).Value.ToString()
            'SRT = "select BarCode, GoodsType, BillBarCode, Unit, Color, Spec, Qty, Description  from BSC_BOM where BillBarCode = '" & BillBarCode & "'"

            SRT = " select BarCode,e.ffullname as  GoodsType, BillBarCode, ISNULL(B.LISTA,'') as Unit ,ISNULL(d.colorCName,'') as Color, ISNULL(c.sizesname,'') as Spec," & vbCrLf
            SRT += " Qty,a.brand,a.supplier,a.SupplierName,Description, BulidDate,BulidMan, BulidComputer from BSC_BOM a  LEFT JOIN BSC_OtherDefineB b ON " & vbCrLf
            SRT += " A.UNIT = B.lISTcODE AND B.itemNo = '7' LEFT JOIN BSC_SizeDefine  c ON A.spec = c.SIZECODE left join " & vbCrLf
            SRT += "BSC_ColorDefine d on a.color =d.colorcode" & vbCrLf
            SRT += "left join BSC_GoodsType e on CAST(a.GoodsType AS int) = e.id " & vbCrLf
            SRT += "where a.BillBarCode = '" & BillBarCode & "'" & vbCrLf
            DT = Exec(SRT, E_ExecType.DataTable)
            If DT.Rows.Count > 0 Then
                Me.CBGoodsType.Text = DT.Rows(0).Item("GoodsType") & ""
                Me.TBBillBarCode.Text = DT.Rows(0).Item("BillBarCode") & ""
                Me.CBColor.Text = DT.Rows(0).Item("Color") & ""
                Me.TBQty.Text = DT.Rows(0).Item("QTY") & ""
                Me.CBUnit.Text = DT.Rows(0).Item("Unit") & ""
                Me.CBSpec.Text = DT.Rows(0).Item("Spec") & ""
                Me.TxtBrand.Text = DT.Rows(0).Item("Brand") & ""
                Me.TxtSupplier.Text = DT.Rows(0).Item("Supplier") & ""
                Me.CmbSupplierName.Text = DT.Rows(0).Item("SupplierName") & ""
                Me.TBDescription.Text = DT.Rows(0).Item("Description") & ""
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message, Msg.CompanyName)
        End Try



    End Sub

    ''' <summary>
    ''' 設置控件的Enable屬性
    ''' </summary>
    ''' <param name="B"></param>
    ''' <remarks></remarks>
    Private Sub EnabledControls(ByVal B As Boolean)
        Me.GroupBox5.Enabled = B
    End Sub

    ''' <summary>
    ''' 設置按鈕狀態
    ''' </summary>
    ''' <param name="type"></param>
    ''' <remarks></remarks>
    Private Sub SetButton(ByVal type As ButtonType, ByVal IsFind As Boolean)
        Try
            Select Case type
                Case ButtonType.Add
                    With MainForm
                        .ToolStripAdd.Enabled = False
                        .ToolStripEdit.Enabled = False
                        .ToolStripDelete.Enabled = False
                        .ToolStripSave.Enabled = True
                        .ToolStripCancel.Enabled = True
                        .ToolStripFirst.Enabled = False
                        .ToolStripLast.Enabled = False
                        .ToolStripNext.Enabled = False
                        .ToolStripPrivous.Enabled = False
                        .ToolStripExport.Enabled = IsFind
                        .ToolStripImport.Enabled = True
                        .ToolStripPrint.Enabled = IsFind
                        .ToolStripFilter.Enabled = True

                    End With

                Case ButtonType.Browse
                    With MainForm
                        .ToolStripAdd.Enabled = True
                        .ToolStripEdit.Enabled = IsFind
                        .ToolStripDelete.Enabled = IsFind
                        .ToolStripSave.Enabled = False
                        .ToolStripCancel.Enabled = False
                        .ToolStripFirst.Enabled = IsFind
                        .ToolStripLast.Enabled = IsFind
                        .ToolStripNext.Enabled = IsFind
                        .ToolStripPrivous.Enabled = IsFind
                        .ToolStripExport.Enabled = IsFind
                        .ToolStripImport.Enabled = True
                        .ToolStripPrint.Enabled = IsFind
                        .ToolStripFilter.Enabled = True

                    End With

                Case ButtonType.Cancel
                    With MainForm
                        .ToolStripAdd.Enabled = True
                        .ToolStripEdit.Enabled = IsFind
                        .ToolStripDelete.Enabled = IsFind
                        .ToolStripSave.Enabled = False
                        .ToolStripCancel.Enabled = False
                        .ToolStripFirst.Enabled = IsFind
                        .ToolStripLast.Enabled = IsFind
                        .ToolStripNext.Enabled = IsFind
                        .ToolStripPrivous.Enabled = IsFind
                        .ToolStripExport.Enabled = IsFind
                        .ToolStripImport.Enabled = True
                        .ToolStripPrint.Enabled = IsFind
                        .ToolStripFilter.Enabled = True

                    End With

                Case ButtonType.Edit
                    With MainForm
                        .ToolStripAdd.Enabled = False
                        .ToolStripEdit.Enabled = False
                        .ToolStripDelete.Enabled = False
                        .ToolStripSave.Enabled = True
                        .ToolStripCancel.Enabled = True
                        .ToolStripFirst.Enabled = False
                        .ToolStripLast.Enabled = False
                        .ToolStripNext.Enabled = False
                        .ToolStripPrivous.Enabled = False
                        .ToolStripExport.Enabled = IsFind
                        .ToolStripImport.Enabled = True
                        .ToolStripPrint.Enabled = IsFind
                        .ToolStripFilter.Enabled = True

                    End With

            End Select
        Catch ex As Exception
            MsgBox(ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try

    End Sub

    ''' <summary>
    ''' 獲取ComBox的值 并填充
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub GetPublic()
        Try

            Dim Table(4) As String
            Dim sql(4) As String
            Dim DSet As DataSet

            '顏色
            Table(0) = "ColorDefine"
            sql(0) = " select ColorCode,ColorCName from dbo.BSC_ColorDefine "

            '單位
            Table(1) = "UnitDefin"
            sql(1) = "select * from (" & _
                     "select ListCode,ListA  from BSC_OtherDefineB " & _
                      "  where ItemNo=(select ID from BSC_OtherDefineA where ItemCode='UnitDefin') )a  order by ListCode"
            '規格
            Table(2) = "Size"
            sql(2) = "select * from (  " & _
                    "select SizeCode,SizeSName from BSC_SizeDefine  )a order by SizeCode"

            '貨品類型
            Table(3) = "GoodsType"
            sql(3) = "select DISTINCT fCode,fFullname from BSC_GoodsType where NoFinished = 1"

            Table(4) = "Supplier"
            sql(4) = "select code,cnname from BSC_supplier"

            DSet = GetDataSet(sql, Table)
            TabSupplier = DSet.Tables(4).Copy
            '填充顏色
            With Me.CBColor
                .DataSource = DSet.Tables("ColorDefine")
                .ValueMember = ("ColorCode")
                .DisplayMember = ("ColorCName")
            End With
            '填充單位
            With Me.CBUnit
                .DataSource = DSet.Tables("UnitDefin")
                .ValueMember = ("ListCode")
                .DisplayMember = ("ListA")
            End With
            '填充規格
            With Me.CBSpec
                .DataSource = DSet.Tables("Size")
                .ValueMember = "SizeCode"
                .DisplayMember = "SizeSName"
            End With
            '填充貨品類型
            With Me.CBGoodsType
                .DataSource = DSet.Tables("GoodsType")
                .ValueMember = "fCode"
                .DisplayMember = "fFullname"
            End With

            '填充供應商
            With Me.CmbSupplierName
                .DataSource = DSet.Tables("Supplier")
                .ValueMember = "code"
                .DisplayMember = "cnname"
                .Text = ""
            End With

        Catch ex As Exception
            MsgBox(ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Sub

    Public Function ActionAdd() As Boolean Implements IDServer.IDServer.IDServer.ActionAdd
        Try
            If AddEnable = False Then
                MessageBox.Show("對不起，您沒有新增權限", Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Information)
                Exit Function
            End If
            If DS.Tables(0).Rows.Count = 0 Then
                Exit Function
            End If
            Me.TabControl1.SelectedIndex = 1
            ActionHelp = IDServer.IDServer.ActionBtn.ActionAdd
            EnabledControls(True)
            SetButton(ButtonType.Add, False)
            Me.TBGoodsType_1.Enabled = True
            Me.CBGoodsType.Enabled = True
            Me.BtnEdit.Enabled = False
            Me.BtnAdd.Enabled = True
            Me.TBBillBarCode.Text = ""
        Catch ex As Exception
        End Try
    End Function

    Public Function ActionCancel() As Boolean Implements IDServer.IDServer.IDServer.ActionCancel
        Try
            Dim strCode As String
            strCode = Me.TBEnName_1.Text
            ClearText()
            EnabledControls(False)

            ActionHelp = IDServer.IDServer.ActionBtn.ActionHelp
            SetButton(ButtonType.Cancel, FindRecord())
            If DS Is Nothing Then
                Exit Function
            End If
            If Me.DGV_Goods.RowCount = 0 Then
                Exit Function
            End If
            FillText(strCode)
            GetToolEnable()
            SetMainMenu(True)
        Catch ex As Exception
            MsgBox("ActionCancel:" + ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Function

    Public Function ActionDelete() As Boolean Implements IDServer.IDServer.IDServer.ActionDelete
        DelData()
    End Function

    Public Function ActionEdit() As Boolean Implements IDServer.IDServer.IDServer.ActionEdit
        EditData(True)
        Me.BtnAdd.Enabled = False
        Me.BtnEdit.Enabled = True
        ''Me.DGV_BomBill_1.ReadOnly = False
    End Function

    Public Function ActionExport() As Boolean Implements IDServer.IDServer.IDServer.ActionExport

    End Function

    Public Function ActionFilter() As Boolean Implements IDServer.IDServer.IDServer.ActionFilter

    End Function

    Public Function ActionGoFirst() As Boolean Implements IDServer.IDServer.IDServer.ActionGoFirst

    End Function

    Public Function ActionGoLast() As Boolean Implements IDServer.IDServer.IDServer.ActionGoLast

    End Function

    Public Function ActionGoNext() As Boolean Implements IDServer.IDServer.IDServer.ActionGoNext

    End Function

    Public Function ActionGoPrevious() As Boolean Implements IDServer.IDServer.IDServer.ActionGoPrevious

    End Function

    Public Function ActionImport() As Boolean Implements IDServer.IDServer.IDServer.ActionImport

    End Function

    Public Function ActionInitialize() As Boolean Implements IDServer.IDServer.IDServer.ActionInitialize

    End Function

    Public Function ActionPrint() As Boolean Implements IDServer.IDServer.IDServer.ActionPrint

    End Function

    Public Function ActionSave() As Boolean Implements IDServer.IDServer.IDServer.ActionSave
        SaveData()
    End Function

    Public Function ActionSelect() As Boolean Implements IDServer.IDServer.IDServer.ActionSelect

    End Function

    Private Sub DGV_Goods_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DGV_Goods.DoubleClick
        Try
            If DS.Tables(0) Is Nothing Then
                Exit Sub
            End If
            If Me.TabControl1.SelectedIndex = 0 Then
                Me.TabControl1.SelectedIndex = 1
            End If
        Catch ex As Exception
            MsgBox("DataGridView1_DoubleClick:" + ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Sub

    ''' <summary>
    ''' 判斷是否有記錄
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    ''' 
    Private Function FindRecord() As Boolean
        If DS Is Nothing Then
            Return False
            Exit Function
        End If

        If Me.DGV_Goods.RowCount = 0 Then
            Return False
        Else
            Return True
        End If

    End Function

    ''' <summary>
    ''' 當選擇物料明細選項卡時
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    ''' 
    Private Sub TabControl1_Selected(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TabControlEventArgs) Handles TabControl1.Selected
        Try
            Me.Cursor = Cursors.WaitCursor

            If ActionHelp = IDServer.IDServer.ActionBtn.ActionAdd Or ActionHelp = IDServer.IDServer.ActionBtn.ActionEdit Then
                Exit Sub
            End If
            If Me.TabControl1.SelectedIndex = 1 Then
                If Me.DGV_Goods.SelectedCells.Count > 0 Then
                    Dim Enname As String = Me.DGV_Goods.Rows(Me.DGV_Goods.CurrentRow.Index).Cells(0).Value.ToString & ""
                    FillText(Enname)
                Else
                    If Not DS Is Nothing Then
                        FillText("")
                    End If
                End If
            End If
        Catch ex As Exception
            Me.Cursor = Cursors.Default
            MsgBox(ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        Finally
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    '從DGV_Goods選定記錄後填充Text
    '從DGV_BomBill填充DGV_BomBill_1
    Private Sub FillText(ByVal Enname As String)
        Try
            ClearText()
            Dim mRow() As DataRow = DS.Tables(0).Select("Enname='" & Enname & "'")
            If mRow Is Nothing Then Exit Sub
            For Each row As DataRow In mRow
                Me.TBBarCode_1.Text = row("Barcode")
                Me.TBEnName_1.Text = row("Enname")
                Me.TBShortName_1.Text = row("shortName")
                Me.TBCnName_1.Text = row("cnname")
                Me.TBSupperItem_1.Text = row("SupperItem")
                Me.TBEnName_2.Text = row("Enname")
            Next
            If Me.DGV_Goods.SelectedCells.Count > 0 Then
                Dim SQLString As String = String.Empty
                Dim DT As DataTable
                DT = New DataTable


                SQLString = " select BarCode,e.fFullName as  GoodsType, BillBarCode, B.LISTA as Unit ,d.colorCName as Color, c.sizesname as Spec," & vbCrLf
                SQLString += " Qty,a.Brand,a.Supplier,a.SupplierName, Description, BulidDate,BulidMan, BulidComputer from BSC_BOM a  LEFT JOIN BSC_OtherDefineB b ON " & vbCrLf
                SQLString += " A.UNIT = B.lISTcODE AND B.itemNo = '7' LEFT JOIN BSC_SizeDefine  c ON A.spec = c.SIZECODE left join " & vbCrLf
                SQLString += "BSC_ColorDefine d on a.color =d.colorcode" & vbCrLf
                SQLString += "left join BSC_GoodsType e on CAST(a.GoodsType AS int) = e.id " & vbCrLf
                SQLString += "where a.BarCode = '" & Enname & "'" & vbCrLf

                DT = Exec(SQLString, E_ExecType.DataTable)

                Me.DGV_BomBill_1.DataSource = DT
                BindDataGridViewCaption(Me.DGV_BomBill_1, Me.ReturnCaption(3))

                If DT.Rows.Count > 0 Then
                    BomTable = DT
                    Me.DGV_BomBill_1.ReadOnly = True
                End If

            End If
        Catch ex As Exception

        End Try
    End Sub

    ''' <summary>
    ''' 清除Text文件
    ''' </summary>
    ''' <remarks></remarks>
    ''' 
    Private Sub ClearText()
        Try
            Me.TBBarCode_1.Text = ""
            Me.TBCnName_1.Text = ""
            Me.TBEnName_1.Text = ""
            Me.TBShortName_1.Text = ""
            Me.TBSupperItem_1.Text = ""
            Me.TBEnName_2.Text = ""
            Me.TBGoodsType_1.Text = ""
            Me.CBGoodsType.SelectedIndex = -1
 
            If DGV_BomBill_1.RowCount > 0 Then
                CType(Me.DGV_BomBill_1.DataSource, DataTable).Clear()
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    Private Sub TabControl1_Selecting(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TabControlCancelEventArgs) Handles TabControl1.Selecting
        Try
            If ActionHelp = IDServer.IDServer.ActionBtn.ActionAdd Or ActionHelp = IDServer.IDServer.ActionBtn.ActionEdit Then
                e.Cancel = True
            End If
        Catch ex As Exception
            MsgBox("TabControl1_Selecting:" + ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Sub

    Private Sub GetToolEnable()
        Try
            With MainForm
                .ToolStripAdd.Enabled = AddEnable
                .ToolStripEdit.Enabled = EditEnable
                .ToolStripDelete.Enabled = DeleteEnable
                .ToolStripPrint.Enabled = PrintEnable
                .ToolStripImport.Enabled = ImportEnable
                .ToolStripExport.Enabled = ExportEnable
                .ToolStripFind.Enabled = FindEnable
                .ToolStripCheck.Enabled = CheckEnable
                .ToolStripUnCheck.Enabled = UncheckEnable
                .ToolStripComplete.Enabled = CompleteEnable
                .ToolStripCheck.Enabled = False
                .ToolStripUnCheck.Enabled = False
                .AddToolStripMenuItem.Enabled = .ToolStripAdd.Enabled
                .Edit1ToolStripMenuItem.Enabled = .ToolStripEdit.Enabled
                .DelToolStripMenuItem.Enabled = .ToolStripDelete.Enabled
                .SaveToolStripMenuItem.Enabled = .ToolStripSave.Enabled
                .CancelToolStripMenuItem.Enabled = .ToolStripCancel.Enabled
                .PrintToolStripMenuItem.Enabled = .ToolStripPrint.Enabled
                .ExportToolStripMenuItem.Enabled = .ToolStripImport.Enabled
                .ImportToolStripMenuItem.Enabled = .ToolStripExport.Enabled
                .FilterToolStripMenuItem.Enabled = .ToolStripFilter.Enabled
            End With

        Catch ex As Exception
            MsgBox("GetToolEnable:" + ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Sub

    ''' <summary>
    ''' 設置主目錄按鈕狀態
    ''' </summary>
    ''' <param name="B"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Private Function SetMainMenu(ByVal B As Boolean)
        Try
            With Me.SelectObject
                .AddToolStripMenuItem.Enabled = B
                .Edit1ToolStripMenuItem.Enabled = B
                .DelToolStripMenuItem.Enabled = B
            End With

        Catch ex As Exception
            MsgBox("SetMainMenu:" + ex.ToString, MsgBoxStyle.Exclamation + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Function

    Private Sub BtnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnAdd.Click
        Try

            If Me.TBEnName_2.Text.Length <= 0 Then
                MessageBox.Show("英文名不能為空！", Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Information)
                Exit Sub
            End If
            If Me.TBBillBarCode.Text.Length <= 0 Then
                MessageBox.Show("物料編號不能為空！", Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Information)
                Me.CBGoodsType.Focus()
                Me.CBGoodsType.DroppedDown = True
                Exit Sub
            End If
            Dim val As Int32 = 0
            If Not Int32.TryParse(Me.TBQty.Text, val) Then
                MessageBox.Show("數量為正整数！", Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Information)
                Me.TBQty.Focus()
                Return
            End If

            Dim NEWROW As DataRow = BomTable.NewRow
            NEWROW("BarCode") = Me.TBEnName_2.Text
            NEWROW("GoodsType") = Me.CBGoodsType.Text
            NEWROW("BillBarCode") = Me.TBBillBarCode.Text
            NEWROW("Unit") = Me.CBUnit.Text
            NEWROW("Color") = Me.CBColor.Text
            NEWROW("Spec") = Me.CBSpec.Text
            NEWROW("Qty") = Me.TBQty.Text
            NEWROW("Brand") = Me.TxtBrand.Text
            NEWROW("Supplier") = Me.TxtSupplier.Text
            NEWROW("SupplierName") = Me.CmbSupplierName.Text
            NEWROW("Description") = Me.TBDescription.Text
            BomTable.Rows.Add(NEWROW)
            BomTable.AcceptChanges()
            Me.DGV_BomBill_1.DataSource = BomTable
            'Me.CBGoodsType.SelectedIndex = ""
            Me.TBBillBarCode.Text = ""
            ' Me.CBColor.SelectedIndex = ""
            'Me.CBSpec.SelectedIndex = ""
            'Me.CBUnit.SelectedIndex = ""
            Me.TBDescription.Text = ""
            Me.TBQty.Text = "1"

        Catch ex As Exception

        End Try
    End Sub

    Private Sub Frm_MauBomBill_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        SetBomTable()
        GetPublic()
        SelectData(False)
        Lode_DGV()
        Me.Panel2.Visible = False
    End Sub

    Private Sub TBGoodsType_1_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TBGoodsType_1.Leave
        Try
            Dim sql As String = String.Empty
            Dim DT As DataTable
            If Me.TBGoodsType_1.Text = "" Then
                Me.CBGoodsType.Text = ""
            Else
                Sql = "select distinct fCode,fCName from bsc_goodstype where fcode = '" & Me.TBGoodsType_1.Text.Trim & "'"
                DT = Exec(Sql, E_ExecType.DataTable)
                If DT.Rows.Count <= 0 Then
                    Me.CBGoodsType.Text = ""
                    Me.TBGoodsType_1.Focus()
                    Me.TBBillBarCode.Text = ""
                    Me.CBGoodsType.DroppedDown = True
                Else
                    Me.CBGoodsType.Text = DT.Rows(0).Item("fCName")
                    Me.CBGoodsType.SelectedValue = DT.Rows(0).Item("fCode")
                    Me.TBBillBarCode.Text = Me.TBEnName_2.Text & "-" & "" & Me.TBGoodsType_1.Text & "001"
                End If

            End If

        Catch ex As Exception

        End Try
    End Sub

    Private Sub CBGoodsType_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CBGoodsType.SelectedIndexChanged
        Try
            If Me.CBGoodsType.Text = "" Or Me.CBGoodsType.Text = "System.Data.DataRowView" Then
                Me.TBGoodsType_1.Text = ""
            Else
                Me.TBGoodsType_1.Text = Me.CBGoodsType.SelectedValue & ""
                Dim Num As String

                Num = GetNum(Me.TBEnName_2.Text.ToString, Me.TBGoodsType_1.Text.ToString).ToString("000")
                Me.TBBillBarCode.Text = Me.TBEnName_2.Text & "-" & "" & Me.TBGoodsType_1.Text & "" & Num & ""
            End If

        Catch ex As Exception
            MsgBox(ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Sub

    ''' <summary>
    ''' 獲取當前的編號的最大值
    ''' </summary>
    ''' <remarks></remarks>
    ''' 

    Public Function GetNum(ByVal BarCode As String, ByVal fCode As String) As Int32
        Try
            Dim Num As Int32
            Dim SQL As String

            BomTable = CType(Me.DGV_BomBill_1.DataSource, DataTable)
            If BomTable.Rows.Count > 0 Then

                Dim rows() As DataRow = BomTable.Select("billbarcode like '" & BarCode & "-" & fCode & "%'")
                Dim max As String = String.Empty
                Dim temp As String = String.Empty
                If rows.Length > 0 Then

                    For Each row As DataRow In rows
                        temp = row("billbarcode")
                        temp = temp.Substring(temp.Length - 3)
                        If String.IsNullOrEmpty(max) Then
                            max = temp
                        Else
                            If (Convert.ToInt32(temp) > Convert.ToInt32(max)) Then
                                max = temp
                            End If
                        End If
                    Next
                Else
                    max = "000"
                End If
                Num = Convert.ToInt32(max)
                Return Num + 1
            Else
                Return 1
            End If
        Catch ex As Exception

        End Try
    End Function

    Private Sub SaveData()
        Dim SQLSTR As String = String.Empty
        Dim DTab As DataTable
        Dim cnSqlServer As New SqlConnection
        cnSqlServer = GetConn()
        Dim cmdSqlServer As New SqlCommand
        Dim trnSqlServer As SqlTransaction = cnSqlServer.BeginTransaction
        Try
            Dim BulidDate As DateTime = Now.ToString("yyyy-MM-dd H:mm:ss")
            Dim BarCode, GoodsType, BillBarCode, Unit, Color, Spec, Qty, Brand, Supplier, SupplierName, Description, BulidMan, BulidComputer As String
            BulidDate = ServerDate
            BulidMan = PubUserName
            BulidComputer = My.Computer.Name.ToString


            If ActionHelp = IDServer.IDServer.ActionBtn.ActionAdd Or ActionHelp = IDServer.IDServer.ActionBtn.ActionEdit Then '添加保存時

                For I As Integer = 0 To Me.DGV_BomBill_1.Rows.Count - 1
                    Dim row As DataGridViewRow = Me.DGV_BomBill_1.Rows(I)

                    BarCode = row.Cells("BarCode").Value & ""
                    GoodsType = row.Cells("GoodsType").Value & ""
                    BillBarCode = row.Cells("BillBarCode").Value & ""
                    Unit = row.Cells("Unit").Value & ""
                    Color = row.Cells("Color").Value & ""
                    Spec = row.Cells("Spec").Value & ""
                    Qty = row.Cells("Qty").Value & ""
                    Brand = row.Cells("Brand").Value & ""
                    Supplier = row.Cells("Supplier").Value & ""
                    SupplierName = row.Cells("SupplierName").Value & ""
                    Description = row.Cells("Description").Value & ""

                    '檢查是否是以前存在的貨品資料

                    SQLSTR = "SELECT *  FROM BSC_GOODS  "
                    SQLSTR += "WHERE "
                    'SQLSTR += " GOODSTYPE IN (SELECT ID FROM DBO.BSC_GOODSTYPE WHERE NOFINISHED <> 1) AND "
                    SQLSTR += " REPLACE(BARCODE,'*','')= '" & BillBarCode & "' "
                    DTab = Exec(SQLSTR, E_ExecType.DataTable)

                    If row.IsNewRow = False Then
                        With cmdSqlServer
                            .Connection = cnSqlServer
                            .Transaction = trnSqlServer
                            .CommandType = CommandType.Text
                            .CommandText = ""
                            ' If ActionHelp = IDServer.IDServer.ActionBtn.ActionEdit Then
                            .CommandText &= " DELETE FROM  BSC_BOM WHERE BillBarCode = '" & BillBarCode & "' and BarCode = '" & BarCode & "';" & vbCrLf
                            'End If

                            '.CommandText = " DELETE FROM  BSC_BOM WHERE BillBarCode = '" & BillBarCode & "';" & vbCrLf
                            '如果加入的是以前的貨品資料，則不需要加
                            'If DTab.Rows.Count <= 0 Then
                            '.CommandText &= "DELETE FROM  BSC_GOODS WHERE REPLACE(BarCode,'*','') = '" & BillBarCode & "';" & vbCrLf
                            ' End If
                            '加入到物料清單表
                            .CommandText &= "INSERT INTO BSC_BOM (BarCode, GoodsType, BillBarCode, Unit, Color, Spec, Qty, brand,supplier,SupplierName,Description, BulidDate, BulidMan, BulidComputer)" & vbCrLf
                            .CommandText &= " SELECT DISTINCT '" & BarCode & "',d.ID,'" & BillBarCode & "',b.ListCode,A.colorCode,c.SizeCode,'" & Qty & "','" & Brand & "','" & Supplier & "','" & SupplierName & "','" & Description & "','" & BulidDate & "','" & BulidMan & "','" & BulidComputer & "'" & vbCrLf
                            .CommandText &= " FROM BSC_ColorDefine a cross join BSC_OtherDefineB b cross join BSC_SizeDefine  c cross join bsc_goodstype d where a.colorcName='" & Color & "' and b.listA = '" & Unit & "' and b.itemNo = '7' and c.sizeSName= '" & Spec & "' and d.ffullname = '" & GoodsType & "'" & vbCrLf

                            '加入到貨品資料表
                            '如果加入的是以前存在的貨品資料呢？則此處不需要再加入
                            If DTab.Rows.Count <= 0 Then
                                .CommandText &= " INSERT INTO BSC_GOODS (BarCode,Code,Color,Spec,ShortName,Cnname,Unit,Status,GoodsType,BulidDate,BulidComputer,BulidBy)" & vbCrLf
                                .CommandText &= " select DISTINCT '*" & BillBarCode & "*','" & BillBarCode & "',A.colorCode,c.SizeCode,'" & GoodsType & "','" & Description & "',b.ListCode,'1',d.ID,'" & BulidDate & "','" & BulidComputer & "','" & BulidMan & "' " & vbCrLf
                                .CommandText &= " FROM BSC_ColorDefine a cross join BSC_OtherDefineB b cross join BSC_SizeDefine  c cross join bsc_goodstype d where a.colorcName='" & Color & "' and b.listA = '" & Unit & "' and b.itemNo = '7' and c.sizeSName= '" & Spec & "' and d.ffullname = '" & GoodsType & "'" & vbCrLf
                            End If
                            .ExecuteNonQuery()
                        End With
                    End If
                Next

            End If

            trnSqlServer.Commit()
            If ActionHelp = IDServer.IDServer.ActionBtn.ActionAdd Then
                MessageBox.Show("新增成功！", Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Information)
            End If
            If ActionHelp = IDServer.IDServer.ActionBtn.ActionEdit Then
                MessageBox.Show("修改成功！", Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Information)
            End If
            Me.DGV_BomBill_1.ReadOnly = True
            SetButton(ButtonType.Browse, True)
            Me.GroupBox5.Enabled = False
            ActionHelp = IDServer.IDServer.ActionBtn.ActionHelp



        Catch ex As Exception
            MsgBox(ex.Message)
            trnSqlServer.Rollback()
            MsgBox("SaveData:" + ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Sub

    Private Sub DGV_Goods_RowPostPaint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewRowPostPaintEventArgs) Handles DGV_Goods.RowPostPaint
        Try
            Dim DGView As DataGridView = DirectCast(sender, DataGridView)
            Dim SBrush As New SolidBrush(DGView.RowHeadersDefaultCellStyle.ForeColor)
            e.Graphics.DrawString(Convert.ToString(e.RowIndex + 1, System.Globalization.CultureInfo.CurrentCulture), e.InheritedRowStyle.Font, SBrush, e.RowBounds.Location.X + 20, e.RowBounds.Location.Y + 4)
        Catch ex As Exception
            MessageBox.Show("DGV_Goods_RowPostPaint:" & ex.Message & ex.StackTrace, Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

    Private Sub DGV_BomBill_RowPostPaint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewRowPostPaintEventArgs) Handles DGV_BomBill.RowPostPaint
        Try
            Dim DGView As DataGridView = DirectCast(sender, DataGridView)
            Dim SBrush As New SolidBrush(DGView.RowHeadersDefaultCellStyle.ForeColor)
            e.Graphics.DrawString(Convert.ToString(e.RowIndex + 1, System.Globalization.CultureInfo.CurrentCulture), e.InheritedRowStyle.Font, SBrush, e.RowBounds.Location.X + 20, e.RowBounds.Location.Y + 4)
        Catch ex As Exception
            MessageBox.Show("DGV_Goods_RowPostPaint:" & ex.Message & ex.StackTrace, Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

    Private Sub DGV_Goods_CellClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DGV_Goods.CellClick
        If Me.DGV_Goods.SelectedCells.Count > 0 Then
            Dim Enname As String = Me.DGV_Goods.Rows(Me.DGV_Goods.CurrentRow.Index).Cells(0).Value.ToString & ""
            Dim SQLString As String = String.Empty
            Dim DT As DataTable
            DT = New DataTable

            SQLString = " select BarCode,e.ffullName as  GoodsType, BillBarCode, B.LISTA as Unit ,d.colorCName as Color, c.sizesname as Spec," & vbCrLf
            SQLString += " Qty,a.Brand,a.Supplier, a.SupplierName,Description, BulidDate,BulidMan, BulidComputer from BSC_BOM a  LEFT JOIN BSC_OtherDefineB b ON " & vbCrLf
            SQLString += " A.UNIT = B.lISTcODE AND B.itemNo = '7' LEFT JOIN BSC_SizeDefine  c ON A.spec = c.SIZECODE left join " & vbCrLf
            SQLString += "BSC_ColorDefine d on a.color =d.colorcode" & vbCrLf
            SQLString += "left join BSC_GoodsType e on CAST(a.GoodsType AS int) = e.id " & vbCrLf
            SQLString += "where a.BarCode = '" & Enname & "'" & vbCrLf


            DT = Exec(SQLString, E_ExecType.DataTable)
            Me.DGV_BomBill.DataSource = DT
            BindDataGridViewCaption(Me.DGV_BomBill, Me.ReturnCaption(2))

        End If
    End Sub
    Private Sub DelData()
        If DeleteEnable = False Then
            MessageBox.Show("對不起，您沒有刪除權限", Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Information)
            Exit Sub
        End If
        If Me.DGV_BomBill_1.Rows.Count > 0 Then
            If MsgBox("確實要刪除：" & Me.TBEnName_1.Text.ToString & "物料清單嗎？", MsgBoxStyle.Question Or MessageBoxButtons.YesNo, Msg.CompanyName) = MsgBoxResult.Yes Then
                Try
                    Dim Sql As String
                    Dim Barcode As String = Me.TBEnName_1.Text
                    'Dim BillBarCode As String = String.Empty
                    Sql = "DELETE FROM  BSC_BOM WHERE BarCode = '" & Barcode & "';"
                    'For I As Integer = 0 To Me.DGV_BomBill_1.Rows.Count - 1
                    '    Dim row As DataGridViewRow = Me.DGV_BomBill_1.Rows(I)
                    '    BillBarCode = row.Cells("BillBarCode").Value

                    '    '檢查是否是以前存在的貨品資料
                    '    Dim SQLSTR As String = String.Empty
                    '    Dim DTab As DataTable
                    '    SQLSTR = "SELECT TOP(100)*  FROM BSC_GOODS  "
                    '    SQLSTR += "WHERE GOODSTYPE IN "
                    '    SQLSTR += "(SELECT ID FROM DBO.BSC_GOODSTYPE WHERE NOFINISHED <> 1) "
                    '    SQLSTR += "AND REPLACE(BARCODE,'*','')= '" & BillBarCode & "' "
                    '    DTab = Exec(SQLSTR, E_ExecType.DataTable)
                    '    If DTab.Rows.Count <= 0 Then
                    '        Sql &= "DELETE FROM  BSC_GOODS WHERE REPLACE(BarCode,'*','') = '" & BillBarCode & "';"
                    '    End If
                    'Next
                    CType(Me.DGV_BomBill_1.DataSource, DataTable).Clear()
                    CType(Me.DGV_BomBill.DataSource, DataTable).Clear()
                    Exec(Sql, E_ExecType.ExecuteNonQuery)
                    MessageBox.Show("刪除成功！", Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Information)
                Catch ex As Exception
                    MessageBox.Show(ex.Message, Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Warning)
                End Try
            End If
        End If
    End Sub

    Private Sub DGV_BomBill_1_RowPostPaint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewRowPostPaintEventArgs) Handles DGV_BomBill_1.RowPostPaint
        Try
            Dim DGView As DataGridView = DirectCast(sender, DataGridView)
            Dim SBrush As New SolidBrush(DGView.RowHeadersDefaultCellStyle.ForeColor)
            e.Graphics.DrawString(Convert.ToString(e.RowIndex + 1, System.Globalization.CultureInfo.CurrentCulture), e.InheritedRowStyle.Font, SBrush, e.RowBounds.Location.X + 20, e.RowBounds.Location.Y + 4)
        Catch ex As Exception
            MessageBox.Show("DGV_Goods_RowPostPaint:" & ex.Message & ex.StackTrace, Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

    Private Sub DGV_Goods_SelectionChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DGV_Goods.SelectionChanged
        If Me.DGV_Goods.SelectedCells.Count > 0 Then
            Dim Enname As String = Me.DGV_Goods.Rows(Me.DGV_Goods.CurrentRow.Index).Cells(0).Value.ToString & ""
            Dim SQLString As String = String.Empty
            Dim DT As DataTable

            SQLString = " select BarCode,e.ffullName as  GoodsType, BillBarCode, B.LISTA as Unit ,d.colorCName as Color, c.sizesname as Spec," & vbCrLf
            SQLString += " Qty,a.Brand,a.Supplier,a.SupplierName, Description, BulidDate,BulidMan, BulidComputer from BSC_BOM a  LEFT JOIN BSC_OtherDefineB b ON " & vbCrLf
            SQLString += " A.UNIT = B.lISTcODE AND B.itemNo = '7' LEFT JOIN BSC_SizeDefine  c ON A.spec = c.SIZECODE left join " & vbCrLf
            SQLString += "BSC_ColorDefine d on a.color =d.colorcode" & vbCrLf
            SQLString += "left join BSC_GoodsType e on CAST(a.GoodsType AS int) = e.id " & vbCrLf
            SQLString += "where a.BarCode = '" & Enname & "'" & vbCrLf


            DT = Exec(SQLString, E_ExecType.DataTable)
            Me.DGV_BomBill.DataSource = DT
            BindDataGridViewCaption(Me.DGV_BomBill, Me.ReturnCaption(2))

        End If
    End Sub

    Private Sub TBENName_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TBENName.KeyDown
        If e.KeyCode = Keys.Enter Then
            BtnFind_Click(Nothing, Nothing)
        End If
    End Sub

    Private Sub TBBarCode_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TBBarCode.KeyDown
        If e.KeyCode = Keys.Enter Then
            BtnFind_Click(Nothing, Nothing)
        End If
    End Sub

    Private Sub BtnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnCancel.Click
        Try
            ActionCancel()
        Catch ex As Exception
            MsgBox(ex.Message, Msg.CompanyName)

        End Try
    End Sub

    Private Sub DGV_BomBill_1_SelectionChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DGV_BomBill_1.SelectionChanged
        FillEdit()
    End Sub

    Private Sub TBBillBarCode_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TBBillBarCode.TextChanged

    End Sub

    Private Sub BtnEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnEdit.Click
        Try
            '修改對應DGV_BomBill_1中的資料
            Dim DTable As DataTable
            Dim BillBarCode As String = Me.TBBillBarCode.Text
            DTable = CType(Me.DGV_BomBill_1.DataSource, DataTable)
            Dim Brow() As DataRow = DTable.Select("BillBarCode='" & BillBarCode & "'")
            If Brow Is Nothing Then Exit Sub
            For Each row As DataRow In Brow
                row("Unit") = Me.CBUnit.Text
                row("Color") = Me.CBColor.Text
                row("Spec") = Me.CBSpec.Text
                row("QTY") = Me.TBQty.Text.ToString
                row("Brand") = Me.TxtBrand.Text.ToString
                row("Supplier") = Me.TxtSupplier.Text.ToString
                row("SupplierName") = Me.CmbSupplierName.Text.ToString
                row("Description") = Me.TBDescription.Text
            Next
            DTable.AcceptChanges()
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub btnOpen1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOpen1.Click
        Try
            selectObj = Me.txtGoodsType1
            strCheckedNode = ""
            With Me.Panel2
                .Visible = Not .Visible

                .Left = Me.txtGoodsType1.Left
                .Top = Me.txtGoodsType1.Top + Me.txtGoodsType1.Height
                .Width = Me.txtGoodsType1.Width
                .Height = Me.TabPage1.Height - .Top - Me.txtGoodsType1.Height - 10
                .Parent = Me.TabPage1
                .BringToFront()
                .AutoScroll = True
            End With

            If Me.Panel2.Visible = True Then
                If Me.TVGoodsType1.Nodes.Count <= 0 Then
                    LoadGoodsType(Me.TVGoodsType1)
                    Me.TVGoodsType1.ExpandAll()

                End If

                If Me.txtGoodsType1.Text.Length > 0 Then
                    Dim strGoodsType As String = "全部\" & Me.txtGoodsType1.Text
                    SelectTreeView(Me.TVGoodsType1, strGoodsType)

                End If

            Else
                '去掉最後一個,
                GetCheckedNode(Me.TVGoodsType1.Nodes)
                If strCheckedNode <> "" Then
                    strCheckedNode = Microsoft.VisualBasic.Left(strCheckedNode, Len(strCheckedNode) - 1)

                End If
                Me.txtGoodsType1.Text = strCheckedNode
            End If


        Catch ex As Exception
            MsgBox(ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Sub

    Private Sub LoadGoodsType(ByVal TV As TreeView)

        Try
            Dim Node As New TreeNode
            Img = New ImageList()
            Dim Company As String = Application.StartupPath & "\pic\35FLOPPY.BMP"
            Dim bumen As String = Application.StartupPath & "\pic\CLSDFOLD.BMP"
            Dim gangwei As String = Application.StartupPath & "\pic\OPENFOLD.BMP"
            Dim Image As Image = Nothing
            Img.Images.Add(Drawing.Image.FromFile(Company))
            Img.Images.Add(Drawing.Image.FromFile(bumen))
            Img.Images.Add(Drawing.Image.FromFile(gangwei))
            TV.ImageList = Img
            Node.Nodes.Clear()
            Node.Tag = "All"
            Node.Text = "全部"
            Node.ImageIndex = 0
            TV.Nodes.Add(Node)
            Dim sql As String = ""
            sql = "select * from dbo.BSC_GoodsType"
            GoodsDs = New DataSet
            GoodsDs = ReturnDataset(sql)
            LoadSecondTree(Node, 0)
            SelectNode = Node
            Node.Expand()
        Catch ex As Exception
            MsgBox("LoadGoodsType:" + ex.ToString, MsgBoxStyle.Exclamation + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Sub

    ''' <summary>
    ''' 選擇TreeView的某個TreeNode
    ''' </summary>
    ''' <param name="aTreeView">需查詢的TreeView</param>
    ''' <param name="strFullPath">需查詢的TreeNode的FullPath</param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Private Function SelectTreeView(ByVal aTreeView As TreeView, ByVal strFullPath As String) As Boolean
        Dim n As TreeNode
        Dim bFind As Boolean = False
        For Each n In aTreeView.Nodes
            bFind = FindSecondNode(aTreeView, n, strFullPath)
            If bFind = True Then
                Exit For
                Exit Function
            Else
                FindSecondNode(aTreeView, n, strFullPath)
            End If
        Next
    End Function

    ''' <summary>
    ''' 獲取選擇的TreeNode,返回一個以，區分的字元串
    ''' </summary>
    ''' <param name="tc">TreeNodeCollection</param>
    ''' <returns>返回一個以，區分的字元串</returns>
    ''' <remarks></remarks>
    ''' 

    Public Function GetCheckedNode(ByVal tc As TreeNodeCollection) As String
        Try
            For Each TNode As TreeNode In tc
                If TNode.Checked = True Then
                    strCheckedNode += "'" + TNode.FullPath.Replace("全部\", "") + "',"
                End If
                GetCheckedNode(TNode.Nodes)
            Next
        Catch ex As Exception
            Return ""
            MsgBox(ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        Finally

        End Try
    End Function

    Private Sub LoadSecondTree(ByVal TreeNode As TreeNode, ByVal ParentID As Integer)
        Try
            If GoodsDs Is Nothing Then Exit Sub
            Dim Rows() As DataRow = GoodsDs.Tables(0).Select("fParentId=" & ParentID)
            Dim Define As Int16 = 0
            For Each mRow As DataRow In Rows
                Dim mNode As New TreeNode
                Define = mRow("fDefine")
                If TreeNode Is Nothing Then
                    mNode.Tag = mRow("ID") & "^" & mRow("fDefine")
                    mNode.Text = "(" & mRow("fCode") & ")" & mRow("fCName")
                    mNode.ImageIndex = 1
                    mNode.SelectedImageIndex = 2
                    TreeNode.Nodes.Add(mNode)
                    mNode.Expand()
                    LoadSecondTree(mNode, mRow("ID"))
                Else
                    mNode.Tag = mRow("ID") & "^" & mRow("fDefine")
                    mNode.Text = "(" & mRow("fCode") & ")" & mRow("fCName")
                    mNode.ImageIndex = 1
                    mNode.SelectedImageIndex = 2
                    TreeNode.Nodes.Add(mNode)
                    mNode.Expand()
                    LoadSecondTree(mNode, mRow("ID"))
                End If
            Next
        Catch ex As Exception
            MsgBox("LoadSecondTree:" + ex.ToString, MsgBoxStyle.Exclamation + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Sub

    ''' <summary>
    ''' 查詢下級TreeNode
    ''' </summary>
    ''' <param name="TV">需查詢的TreeView</param>
    ''' <param name="n">需查詢的TreeNode</param>
    ''' <param name="strFullPath">需查詢的TreeNode的FullPath</param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    ''' 

    Private Function FindSecondNode(ByVal TV As TreeView, ByVal n As TreeNode, ByVal strFullPath As String) As Boolean

        Dim aNode As TreeNode
        For Each aNode In n.Nodes
            If aNode.FullPath = strFullPath Then
                TV.Focus()
                TV.SelectedNode = aNode

                Return True
                Exit For
                Exit Function
            Else
                FindSecondNode(TV, aNode, strFullPath)
            End If

        Next
    End Function

    Private Sub TVGoodsType1_AfterCheck(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles TVGoodsType1.AfterCheck
        Try
            Me.Cursor = Cursors.WaitCursor
            GetNodeCheck(e.Node.Nodes, e.Node.Checked)
        Catch ex As Exception
            Me.Cursor = Cursors.Default
            MsgBox(ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        Finally
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    ''' <summary>
    ''' 設置TreeView的選中狀態
    ''' </summary>
    ''' <param name="tc">TreeNodeCollection</param>
    ''' <param name="Check">選中狀態：True或False</param>
    ''' <remarks></remarks>
    ''' 

    Public Sub GetNodeCheck(ByVal tc As TreeNodeCollection, ByVal Check As Boolean)
        For Each TNode As TreeNode In tc
            TNode.Checked = Check
            GetNodeCheck(TNode.Nodes, Check)
        Next

    End Sub

    Private Sub TVGoodsType1_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TVGoodsType1.DoubleClick
        Try
            If Me.TVGoodsType1.SelectedNode IsNot Nothing Then
                'If Me.TVGoodsType1.SelectedNode.GetNodeCount(False) > 0 Then
                '    MsgBox(GetMsg("BSC", "GoodsTypeNotDetail"), MsgBoxStyle.Information + MsgBoxStyle.OkOnly, Msg.CompanyName)
                '    Me.TVGoodsType1.SelectedNode.ExpandAll()
                '    Exit Sub

                'End If
                Select Case selectObj.Name
                    'Case "txtGoodsType"
                    '    Me.txtGoodsType.Text = Me.TVGoodsType.SelectedNode.FullPath.Replace("全部\", "")
                    '    Me.txtGoodsType.Tag = Me.TVGoodsType.SelectedNode.Tag.ToString.Split("^")(0)
                    '    Me.Panel7.Visible = False
                    Case "txtGoodsType1"
                        Me.txtGoodsType1.Text = Me.TVGoodsType1.SelectedNode.FullPath.Replace("全部\", "")
                        Me.txtGoodsType1.Tag = Me.TVGoodsType1.SelectedNode.Tag.ToString.Split("^")(0)
                        Me.Panel2.Visible = False

                End Select

            End If
        Catch ex As Exception
            MsgBox(ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Sub

    Private Sub BtnSetSpec_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnSetSpec.Click
        Try

            Dim Spectxt As String
            Dim FrmSetOtherDefine As New Frm_SetOtherDefine
            FrmSetOtherDefine.SizeTB = CType(CBSpec.DataSource, DataTable)

            FrmSetOtherDefine.ShowDialog()
            CBSpec.DataSource = FrmSetOtherDefine.SizeTB
            If FrmSetOtherDefine.BtnSizeYes.DialogResult = Windows.Forms.DialogResult.OK Then
                Spectxt = FrmSetOtherDefine.txtSizeSimple.Text
                Me.CBSpec.Text = Spectxt
            End If


        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Sub

    Private Sub BtnSetColor_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnSetColor.Click
        Try
            Dim Colortxt As String
            Dim FrmSetColor As New Frm_SetColor
            FrmSetColor.ColorTB = CType(Me.CBColor.DataSource, DataTable)
            FrmSetColor.ShowDialog()
            Me.CBColor.DataSource = FrmSetColor.ColorTB
            If FrmSetColor.BtnSizeYes.DialogResult = Windows.Forms.DialogResult.OK Then
                Colortxt = FrmSetColor.txtSizeSimple.Text
                Me.CBColor.Text = Colortxt
            End If
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Sub

    Private Sub BtnSetUnit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnSetUnit.Click

        Try
            Dim Unittxt As String
            Dim FrmSetUNit As New Frm_SetUnit
            FrmSetUNit.UnitTB = CType(Me.CBUnit.DataSource, DataTable)
            FrmSetUNit.ShowDialog()
            Me.CBUnit.DataSource = FrmSetUNit.UnitTB
            If FrmSetUNit.BtnSizeYes.DialogResult = Windows.Forms.DialogResult.OK Then
                Unittxt = FrmSetUNit.txtSizeSimple.Text
                Me.CBUnit.Text = Unittxt
            End If
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Sub

    Private Sub txtGoodsType1_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtGoodsType1.KeyDown
        If e.KeyCode = Keys.Enter Then
            BtnFind_Click(Nothing, Nothing)
        End If
    End Sub

    Private Sub CBSpec_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
        'GetPublic()
    End Sub

    Private Sub DGV_Goods_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DGV_Goods.CellContentClick

    End Sub

    ''' <summary>
    ''' 雙擊查找以前存在的貨品做物料
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks> 
    Private Sub TBBillBarCode_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TBBillBarCode.DoubleClick
        If ActionHelp = IDServer.IDServer.ActionBtn.ActionAdd Then
            Try
                Dim CBGoodsType As String
                Dim TBBillBarCode As String
                Dim CBColor As String
                Dim CBSpec As String
                Dim CBUnit As String
                Dim TBDescription As String
                Dim FrmSelectGoods As New Frm_SelectGood
                Dim DT As DataTable

                FrmSelectGoods.ShowDialog()
                If FrmSelectGoods.Iscancel = False Then

                    Me.CBGoodsType.Text = FrmSelectGoods.dgrdGoods.Rows(FrmSelectGoods.dgrdGoods.CurrentRow.Index).Cells(8).Value.ToString & ""
                    Me.TBBillBarCode.Text = FrmSelectGoods.dgrdGoods.Rows(FrmSelectGoods.dgrdGoods.CurrentRow.Index).Cells(0).Value.ToString & ""
                    Me.CBColor.Text = FrmSelectGoods.dgrdGoods.Rows(FrmSelectGoods.dgrdGoods.CurrentRow.Index).Cells(4).Value.ToString & ""
                    Me.CBSpec.Text = FrmSelectGoods.dgrdGoods.Rows(FrmSelectGoods.dgrdGoods.CurrentRow.Index).Cells(5).Value.ToString & ""
                    Me.CBUnit.Text = FrmSelectGoods.dgrdGoods.Rows(FrmSelectGoods.dgrdGoods.CurrentRow.Index).Cells(6).Value.ToString & ""
                    Me.TBDescription.Text = FrmSelectGoods.dgrdGoods.Rows(FrmSelectGoods.dgrdGoods.CurrentRow.Index).Cells(2).Value.ToString & ""
                    Me.TxtBrand.Text = FrmSelectGoods.dgrdGoods.Rows(FrmSelectGoods.dgrdGoods.CurrentRow.Index).Cells("Brand").Value.ToString & ""
                    Me.TxtSupplier.Text = FrmSelectGoods.dgrdGoods.Rows(FrmSelectGoods.dgrdGoods.CurrentRow.Index).Cells("Suppcode").Value.ToString & ""
                    Me.CmbSupplierName.Text = FrmSelectGoods.dgrdGoods.Rows(FrmSelectGoods.dgrdGoods.CurrentRow.Index).Cells("Supplier").Value.ToString & ""

                End If
            Catch ex As Exception
                MessageBox.Show(ex.Message, Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Information)
            End Try

        End If
    End Sub

    ''' <summary>
    ''' 刪除其中一項物料
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub DelToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DelToolStripMenuItem.Click
        Try
            Dim BarCode As String
            Dim BillBarCode As String
            BarCode = Me.DGV_BomBill_1.Rows(Me.DGV_BomBill_1.CurrentRow.Index).Cells(0).Value.ToString & ""
            BillBarCode = Me.DGV_BomBill_1.Rows(Me.DGV_BomBill_1.CurrentRow.Index).Cells(2).Value.ToString & ""
            If MsgBox("確實要刪除：" & BarCode & "物料" & BillBarCode & "嗎？", MsgBoxStyle.Question Or MessageBoxButtons.YesNo, Msg.CompanyName) = MsgBoxResult.Yes Then
                Dim Sql As String = "DELETE FROM  BSC_BOM WHERE BarCode = '" & BarCode & "' and BillBarCode = '" & BillBarCode & "';"
                Exec(Sql, E_ExecType.ExecuteNonQuery)
                Me.DGV_BomBill_1.Rows.Remove(Me.DGV_BomBill_1.CurrentRow)
            End If

        Catch ex As Exception
            MessageBox.Show(ex.Message, Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Information)
        End Try
    End Sub

    Private Sub TBENName_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TBENName.TextChanged

    End Sub

    Private Sub TBBarCode_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TBBarCode.TextChanged

    End Sub


    Private Sub CmbSupplierName_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles CmbSupplierName.SelectedIndexChanged
        '
    End Sub

    Private Sub TxtSupplier_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TxtSupplier.TextChanged
        Try
            Dim TabSupp() As DataRow = TabSupplier.Select("code='" & Me.TxtSupplier.Text.ToString & "'")
            If TabSupp.Length > 0 Then
                For Each row As DataRow In TabSupp
                    Me.CmbSupplierName.SelectedValue = row("code")
                Next
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message, Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Information)
        End Try
    End Sub

    Private Sub CmbSupplierName_SelectedValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles CmbSupplierName.SelectedValueChanged

        Try
            Dim TabSupp() As DataRow = TabSupplier.Select("code='" & Me.CmbSupplierName.SelectedValue.ToString & "'")
            If TabSupp.Length > 0 Then
                For Each row As DataRow In TabSupp
                    Me.TxtSupplier.Text = row("code")
                Next
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message, Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Information)
        End Try
    End Sub

    Private Sub GroupBox3_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GroupBox3.Enter

    End Sub
End Class

